package com.paomi.client.activity.detail.adapter;

import android.app.Activity;
import android.support.annotation.NonNull;
import android.support.v7.widget.RecyclerView;
import android.text.Html;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.TextView;

import com.paomi.client.R;
import com.paomi.client.activity.reply.model.ReplyContent;
import com.paomi.client.utils.Util;

import java.util.ArrayList;
import java.util.List;

import butterknife.BindView;
import butterknife.ButterKnife;

/**
 * Created by pc on 2018/10/29
 * Describe:
 * com.paomi.client.activity.detail.adapter
 */
public class ReplyItemAdapter extends RecyclerView.Adapter<ReplyItemAdapter.ViewHolder> {


    private Activity mcontext;
    private List<ReplyContent> ticketList = new ArrayList<>();
    private OnItemClick onItemClick;
    public void OnItemClick(OnItemClick onclick){
        onItemClick=onclick;
    }

    public ReplyItemAdapter(Activity comtext) {
        mcontext = comtext;
    }

    public void setData(List<ReplyContent> jsonList) {
        ticketList = jsonList;
        notifyDataSetChanged();
    }

    @NonNull
    @Override
    public ViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
        return new ViewHolder(LayoutInflater.from(mcontext).inflate(R.layout.item_comment_reply, parent, false));
    }

    @Override
    public void onBindViewHolder(@NonNull ViewHolder holder, int position) {
        ReplyContent comment=ticketList.get(position);
        holder.content_name.setText(comment.getUserName());
        String str="";
        if(comment.getReplyUserId()!=0 ){//判断回复的是主评论，还是子评论 回复的是主评论 为0   ~&& comment.getReplyId()!=0
            holder.reply_name.setText(comment.getReplyUserName());
            str=mcontext.getResources().getString(R.string.msg_reply)+"<font color='#108EE9'>"+"@"+comment.getReplyUserName()+"</font>"+"："+comment.getContent();
        }else{
            holder.reply_name.setText("");
            str=comment.getContent();
        }

        holder.tv_content.setText(Html.fromHtml(str));
        Util.setWidthAndHeight(holder.content_line,Util.dip2px(mcontext,3),Util.dip2px(mcontext,80));


        //回复的点击监听
        holder.tv_content.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if(onItemClick!=null){
                    onItemClick.setOnContentClick(holder.tv_content,position);
                }
            }
        });
        holder.itemView.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {

            }
        });
    }

    @Override
    public int getItemCount() {
        return ticketList.size();
    }

    public class ViewHolder extends RecyclerView.ViewHolder {

        @BindView(R.id.content_line)
        View content_line;
        @BindView(R.id.content_name)
        TextView content_name;
        @BindView(R.id.btn_content)
        TextView btn_content;
        @BindView(R.id.tv_content)
        TextView tv_content;
        @BindView(R.id.reply_name)
        TextView reply_name;

        public ViewHolder(View itemView) {
            super(itemView);
            ButterKnife.bind(this, itemView);
        }
    }
    public interface OnItemClick {
        void setOnContentClick(View view, int pos);
    }
}
